<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <div>
      <el-form-item label="工作说明">
         <el-input v-model="listQuery.fworkmemo" clearable class="filter-item" placeholder="请输入工作说明"/>
      </el-form-item>
      <!--<el-input v-model="listQuery.fsiteNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入所属学校"/>-->
      <!--<el-input v-model="listQuery.fpointNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入实验室编号"/>-->
      <el-form-item label="实验室">
        <div style="display:flex;justify-content:flex-start;align-items:center">
          <el-select style="display: inline-block" clearable filterable v-model="listQuery.fpointNo" class="filter-item" placeholder="请选择实验室">
            <el-option
              v-for="item in getListfpointNo"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <i class="el-icon-search selectI" @click="searchDialoglab('search')"/>
        </div>
      </el-form-item>
         <el-form-item label="设定人员">
        <el-input v-model="listQuery.fplanuser" clearable class="filter-item" placeholder="请输入设定人员"/>
      </el-form-item>
        <el-form-item class="searchdate" label="设定日期">
          <el-date-picker v-model="listQuery.fplandate" clearable class="filter-item" type="date" placeholder="请输入设定日期"></el-date-picker>
      </el-form-item>
      </div>
      <div>
      <el-form-item style="margin-left:28px" label="备注">
        <el-input v-model="listQuery.fexplanation" clearable class="filter-item"  placeholder="请输入备注"/>
      </el-form-item>
       <el-form-item>
          <el-button round class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" @click="handleFilter">查找</el-button><!--v-permission="[':planHead:read']" -->
          <el-button round class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" @click="handleCreate">添加</el-button><!--v-permission="[':planHead:create']" -->
       </el-form-item>
       </div>
      <!--<el-input v-model="listQuery.foperator" clearable class="filter-item" style="width: 200px;" placeholder="请输入操作员"/>
      <el-date-picker v-model="listQuery.fupddate" type="datetime" placeholder="请输入修改时间"></el-date-picker>
      <el-date-picker v-model="listQuery.fcheckdate" type="datetime" placeholder="请输入确认时间"></el-date-picker>
      <el-input v-model="listQuery.fcheckuser" clearable class="filter-item" style="width: 200px;" placeholder="请输入确认员"/>
        <el-input v-model.number="listQuery.fbillstate" clearable class="filter-item" style="width: 200px;" placeholder="请输入单据状态"/>-->
    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightTwo" v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">
       <el-table-column :width="buttonColor.operationThree" align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{color:buttonColor.edit,cursor:'pointer'}" @click="handleUpdate(scope.row)">
            <img src="../../../public/images/edit.png" />
            编辑
          </div>
          <div v-if="scope.row.fbillstate === 1" :style="{color:buttonColor.delete,cursor:'pointer'}" @click="handleDelete(scope.row)">
            <img src="../../../public/images/del.png" />
            删除
          </div>
          <div v-else :style="{color:buttonColor.delete,cursor:'pointer'}">
            <img src="../../../public/images/del.png" />
            删除
          </div>
            <div v-if="scope.row.fbillstate === 1" :style="{color:buttonColor.examine,cursor:'pointer'}" @click="audit(scope.row)">
            <img src="../../../public/images/examine.png" />
            确认
          </div>
           <div v-else :style="{color:buttonColor.examine,cursor:'pointer'}">
            <img src="../../../public/images/examine.png" />
            确认
          </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :index="indexMethod" align="center" :width="buttonColor.line" label="序" type="index" />
      <el-table-column align="left" label="工作说明" prop="fworkmemo" />
      <el-table-column :width="buttonColor.date" :formatter="datetimeFormat2" align="left" label="设定日期" prop="fplandate" />
      <el-table-column :formatter="formatforduser" align="left" label="设定人员" prop="fplanuser" />
      <el-table-column :formatter="formatfsiteNo" align="left" label="所属学校" prop="fsiteNo" />
      <el-table-column :formatter="formatfpointNo" align="left" label="实验室" prop="fpointNo" />
      <el-table-column align="left" label="备注" prop="fexplanation" />
      <!--<el-table-column align="right" label="操作员" prop="foperator" />-->
      <el-table-column :width="buttonColor.date" :formatter="datetimeFormat" align="left" label="修改时间" prop="fupddate" />
      <!--<el-table-column :formatter="datetimeFormat" align="right" label="确认时间" prop="fcheckdate" />
      <el-table-column align="right" label="确认员" prop="fcheckuser" />-->
      <!--<el-table-column align="right" label="单据状态" prop="fbillstate" />
      <el-table-column align="center" label="操作员" prop="foperator" />-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <!--<el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
      width="98%"
      top="2vh"
    >
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="80px"
      >
      <!--<el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">-->
        <el-row>
          <el-col :span="6">
            <el-form-item label="工作单号" prop="fbillno">
              <el-input v-model="dataForm.fbillno"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="required" label="工作说明" prop="fworkmemo">
              <el-input v-model="dataForm.fworkmemo"/>
            </el-form-item>
          </el-col>
            <el-col :span="6">
            <el-form-item class="required" label="所属学校" prop="fsiteNo">
              <!--<el-input v-model="dataForm.fsiteNo"/>-->
              <el-select :disabled='true' @change="fsiteNoChange" v-model="dataForm.fsiteNo" clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
              <el-form-item class="required" label="设定日期" prop="fplandate">
              <el-date-picker v-model="dataForm.fplandate" type="date"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
             <el-form-item
              v-if="this.$store.state.user.fsiteLevel==3"
              label="实验室"
              prop="fpointNo"
            >
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.fpointNo" class="filter-item" placeholder="请选择实验室" @change="listqualityEqt(dataForm.fpointNo)">
                  <el-option
                    v-for="item in getListfpointNo"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
                </el-select>
                <i class="el-icon-search selectI" @click="searchDialoglab('add')"/>
              </div>
            </el-form-item>
            <el-form-item
              v-else
              label="实验室"
              prop="fpointNo"
            >
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.fpointNo" class="filter-item" placeholder="请选择实验室" @change="listqualityEqt(dataForm.fpointNo)">
                  <el-option
                    v-for="item in getListfpointNoAreaAdd"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
                </el-select>
                <i class="el-icon-search selectI" @click="searchDialoglab('add')"/>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item class="text" style="width:95%" label="备注" prop="fexplanation">
              <el-input v-model="dataForm.fexplanation"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-row>
          <el-col :span="12">
            <el-form-item label="设定人员" prop="fplanuser">
              <el-input v-model="dataForm.fplanuser"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            &lt;!&ndash;<el-form-item label="操作员" prop="foperator">
              <el-input v-model="dataForm.foperator"/>
            </el-form-item>
            <el-form-item label="修改时间" prop="fupddate">
              <el-date-picker v-model="dataForm.fupddate" type="datetime"/>
            </el-form-item>&ndash;&gt;
            <el-form-item label="确认时间" prop="fcheckdate">
              <el-date-picker v-model="dataForm.fcheckdate" type="datetime"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="确认员" prop="fcheckuser">
              <el-input v-model="dataForm.fcheckuser"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据状态" prop="fbillstate">
              <el-input v-model.number="dataForm.fbillstate"/>
            </el-form-item>
          </el-col>
        </el-row>-->
      </el-form>

      <div :style="{width:'100%',display:'flex',border: '2px solid #F2F2F2',height:panelHeight}">
        <div style="width:343px;">
             <div v-if="activeName === 'first'" style="margin-top:70px">
                 <el-form ref="dataForm1" :rules="rules1" :model="dataForm1" status-icon label-position="left" label-width="auto">
                  <!--<el-form-item label="序号" prop="fentryid">
                    <el-input v-model.number="dataForm1.fentryid"/>
                  </el-form-item>-->
                   <el-form-item class="required" label="智量仪" prop="fequipmentbarcode">
                     <el-select clearable filterable v-model="dataForm1.fequipmentbarcode" class="filter-item" placeholder="请选择智量仪">
                       <el-option
                         v-for="item in zlyList"
                         :key="item.fequipmentbarcode"
                         :label="item.fequipmenttag"
                         :value="item.fequipmentbarcode"/>
                     </el-select>
                   </el-form-item>
                  <el-form-item class="required" label="物料编号" prop="fgoodno">
                    <div
                      style="
                            display: flex;
                            justify-content: flex-start;
                            align-items: center;
                          "
                    >
                      <el-input
                        v-model="dataForm1.fgoodno"
                        style="display: inline-block"
                      />
                      <i
                        class="el-icon-search selectI"
                        @click="searchDialogMaterial()"
                      />
                    </div>
                  </el-form-item>
                  <el-form-item class="required" label="物料名称" prop="fgoodname">
                    <el-input disabled v-model="dataForm1.fgoodname" />
                  </el-form-item>
                  <el-form-item class="required" label="速度档位" prop="fspeed">
                    <el-select v-model="dataForm1.fspeed" class="filter-item" placeholder="请选择速度档位">
                        <el-option v-for="item in fspeedList" :key="item.value" :label="item.label" :value="item.value"/>
                      </el-select>
                  </el-form-item>
                  <el-form-item class="required" label="单位" prop="funit">
                    <!--<el-input v-model.number="dataForm1.funit"/>-->
                    <!--<el-form-item class="el-form-item-small" label="地区类型" prop="fsiteLevel">-->
                      <el-select v-model="dataForm1.funit" class="filter-item" placeholder="请选择单位">
                        <el-option v-for="item in funitList" :key="item.value" :label="item.label" :value="item.value"/>
                      </el-select>
                    <!--</el-form-item>-->
                  </el-form-item>
                  <el-form-item class="required" label="设定重量" prop="fsetweight">
                    <el-input v-model.number="dataForm1.fsetweight"/>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: center">
                  <el-button round size="medium" :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="cancle">取消</el-button>
                  <el-button round size="medium"  :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="addData">确定</el-button>
                </div>
             </div>
             <div v-if="activeName === 'second'" :style="{marginTop:'20px'}">
                 <el-form ref="dataForm2" :model="atsUsers" status-icon label-position="left" label-width="auto">
                <el-table ref="multipleTable" :height="panelHeightUser"  :style="{width:'100%',overflow: 'auto'}" :data="atsUsers" tooltip-effect="dark" @selection-change="handleSelectionChange">
                  <el-table-column type="selection" width="55"/>
                  <el-table-column align="left" width="100" label="人员编号" prop="fuserno" />
                  <el-table-column align="left" label="人员名称" prop="username" />
                </el-table>
              </el-form>
              <div slot="footer" class="dialog-footer" style="text-align: center;margin-top:20px">
                  <el-button round size="medium" :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="cancle1">取消</el-button>
                  <el-button round size="medium"  :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="addData2">确定</el-button>
              </div>
             </div>
        </div>
          <el-tabs style="width:calc(100% - 343px)" type="border-card"  v-model="activeName" @tab-click="handleClick">
        <el-tab-pane name="first" label="物料列表">
          <div class="filter-container">
            <el-table :height="panelHeight" v-loading="listLoading" :data="dataForm.details" element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">

              <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
              <!--<el-table-column align="right" label="序号" prop="fentryid" />-->
              <el-table-column align="right" label="固定资产码" prop="fequipmentbarcode" />
              <el-table-column :formatter="formatfpduser" align="right" label="设备标签" prop="fequipmentbarcode" />
              <el-table-column align="right" label="物料编号" prop="fgoodno" />
              <el-table-column
                :formatter="formatfgoodno"
                align="center"
                label="物料名称"
                prop="fgoodname"
              />
              <el-table-column align="right" label="速度档位" prop="fspeed" />
              <el-table-column align="right" label="设定重量" prop="fsetweight" />
              <el-table-column align="right" label="单位" prop="funit" >
                <template slot-scope="scope">
                  <el-tag>{{ scope.row.funit | siteTypeFilter }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" width="140" label="操作" class-name="small-padding">
                <template slot-scope="scope">
                   <div class="operationButton">
                      <div :style="{color:buttonColor.delete,cursor:'pointer'}" @click="handleDelete1(scope.row)">
                        <img src="../../../public/images/del.png" />
                        删除
                      </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
        <el-tab-pane name="second" label="人员列表">
          <div class="filter-container">
            <el-table :height="panelHeight" v-loading="listLoading" :data="dataForm.details2" element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">

              <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
              <el-table-column align="right" label="人员编号" prop="userid" />
              <el-table-column :formatter="formatfpduser2" align="right" label="人员名称" prop="userid" />
              <el-table-column align="center" width="140" label="操作" class-name="small-padding">
                <template slot-scope="scope">
                   <div class="operationButton">
                      <div :style="{color:buttonColor.delete,cursor:'pointer'}" @click="handleDelete2(scope.row)">
                        <img src="../../../public/images/del.png" />
                        删除
                      </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button round :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="cancel()">取消</el-button>
        <el-button round v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button round v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>


    <labDialog :searchVisiblelab.sync="searchVisiblelab" :fsiteNo.sync="labDialogFsiteNo" :tableColor="tableColor" @labDialog="labDialogValue" />
    <material
      :materialIdVisible.sync="materialIdVisible"
      :fsiteNo.sync="materialFsiteNo"
      :tableColor="tableColor"
      @materialDialog="materialDialogValue"
    />
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listplanHead, createplanHead, updateplanHead, deleteplanHead, listqualityEqtByFpointNo } from '@/api/planHead'
import { fsiteOptionsByLevel,getListArea, getFequipmentbarcode,getPointOptions,getFbillno} from '@/api/area'
import { listUserByFsiteNo } from '@/api/airAlarm'
import { checkBiz } from '@/api/area'
import { tableColor, titleColor,buttonColor } from '@/filters/index'
import LabDialog from '@/components/LabDialog'
import { listRoom } from '@/api/room'
import Material from "@/components/Material";
import Pagination from '@/components/Pagination'
import moment from 'moment'

const siteTypeMap = {
  0: 'g',
  1: 'ml'
}

export default {
  name: 'ITcPlanHead',
  components: { Pagination, LabDialog, Material },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type]
    }
  },
  data() {
    return {
      selectList: [],
      fspeedList:[
         { value: 1, label: "1" },
         { value: 2, label: "2" },
         { value: 3, label: "3" },
         { value: 4, label: "4" },
         { value: 5, label: "5" },
         { value: 6, label: "6" },
      ],
      funitList:[
         { value: 0, label: "g" },
         { value: 1, label: "ml" },
      ],
      panelHeight: 0,
      panelHeightUser: 0,
      activeName: 'first',
      buttonColor,
      getFsiteNo:null,
      labFlag:null,
      labDialogFsiteNo:null,
      selectListlab:[],
      searchVisiblelab: false,
      materialIdVisible: false,
      materialFsiteNo: null,
      searchListlab: [],
      goodAllList: [],
      listLoadingSearchlab: true,
      totalSearchlab: 0,
      listQuerySearchlab: {
        page: 1,
        limit: 20,
        fpointName: undefined,
        sort: 'create_time',
        order: 'desc'
      },

      searchFlaglab:'',
      siteTypeMap,
      getListforduser:null,
      fstateOptions: null,
      getListfclassstno: null,
      getListfequipmenttype: null,
      getListfsiteNo: null,
      getListfpointNo: null,
      getListfpointNoQuery: null,
      getListfpointNoAll: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fworkmemo: undefined,
        fplandate: undefined,
        fplanuser: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fexplanation: undefined,
        foperator: undefined,
        fupddate: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      searchListAll: [],
      searchList: [],
      searchVisible: false,
      totalSearch: 0,
      listLoadingSearch: true,
      listQuerySearch: {
        page: 1,
        limit: 20,
        fequipmentno: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        fbillno: undefined,
        fworkmemo: undefined,
        fplandate: undefined,
        fplanuser: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fexplanation: undefined,
        foperator: undefined,
        fupddate: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        id: undefined,
        details: [],
        details2: []
      },
      tableColor,
      titleColor,
      atsUsers: null,
      details2: null,
      zlyList: null,
      dataForm1: {
        fequipmentbarcode: undefined,
        fgoodno: undefined,
        fgoodname: undefined,
        fspeed: undefined,
        funit: undefined,
        fsetweight: undefined,
      },
      dataForm2: {
        fuserno: undefined,
        username: undefined,
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogStatus: '',
      dialogStatus1: '',
      dialogStatus2: '',
      detailsTmp: [],
      detailsTmp2: [],
      multipleSelection: [],
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fworkmemo: [
          { required: true, message: '工作说明不能为空' },
        ],
        fplandate: [
          { required: true, message: '设定日期不能为空' },
        ],
        fplanuser: [
          { required: true, message: '设定人员不能为空' },
        ],
        fsiteNo: [
          { required: true, message: '所属学校不能为空' },
        ],
        foperator: [
          { required: true, message: '操作员不能为空' },
        ],
        fupddate: [
          { required: true, message: '修改时间不能为空' },
        ],
        fbillstate: [
          { type: 'number', message: '单据状态必须为数字值' },
        ],
      },
      rules1:{
         fequipmentbarcode: [
          { required: true, message: '智量仪不能为空' },
        ],
        fgoodno: [
          { required: true, message: '物料编号不能为空' },
        ],
        fspeed: [
          { required: true, message: '速度档位不能为空' },
        ],
        funit: [
          { required: true, message: '单位不能为空' },
        ],
        fsetweight: [
          { required: true, message: '设定重量不能为空' },
        ],
      }
    }
  },
  created() {
    this.panelHeight = document.documentElement.clientHeight-294
    this.panelHeightUser = document.documentElement.clientHeight-354
    this.getList()

    //学校
    fsiteOptionsByLevel(3)
      .then(response => {
        this.getListfsiteNo = response.data.data.list
      })
      .catch(() => {
        this.getListfsiteNo = []
      })
    // 物料
    getListArea({ flag: 31 })
      .then((response) => {
        this.goodAllList = response.data.data.list;
      })
      .catch(() => {
        this.goodAllList = [];
      });

    getListArea({flag:13})
      .then(response => {
        this.getListforduser = response.data.data.list
      })
      .catch(() => {
        this.getListforduser = []
      })
    if(this.$store.state.user.fsiteLevel==3){
      this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
      // 实验室
      getListArea({ flag: 10,arg1:this.$store.state.user.fsiteNo })
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
    }else{
      // 实验室
      getListArea({ flag: 10 })
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
    }

  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    datetimeFormat2(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    formatfsiteNo: function(row, column) {
      if (this.getListfsiteNo) {
        for (let i = 0; i < this.getListfsiteNo.length; i++) {
          if (row.fsiteNo === this.getListfsiteNo[i].value) {
            return this.getListfsiteNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },

      // 切换tab子标签
      handleClick(tab, event) {
        console.log('tab.name',tab.name);
        this.activeName = tab.name
        // if(tab.name === 'second'){
        //   this.handleUpdate2()
        // }else if(tab.name === 'fourth'){
        //   this.handleUpdate3()
        // }else if(tab.name === 'first'){
        //   this.handleUpdate1()
        // }
    },
    // 实验室
    formatfpointNo: function(row, column) {
      if (this.getListfpointNo) {
        for (let i = 0; i < this.getListfpointNo.length; i++) {
          if (row.fpointNo === this.getListfpointNo[i].value) {
            return this.getListfpointNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },

    // 人员
    formatforduser: function(row, column) {
      if(this.getListforduser){
        for (let i = 0; i < this.getListforduser.length; i++) {
          if (row.fplanuser == this.getListforduser[i].value) {
            return this.getListforduser[i].label
          }
        }
        return ''
      }else{
        return ''
      }
    },
    // 设备标签
    formatfpduser: function(row, column) {
      if(this.details2){
        for (let i = 0; i < this.details2.length; i++) {
          if (row.fequipmentbarcode == this.details2[i].fequipmentbarcode) {
            return this.details2[i].fequipmenttag
          }
        }
        return ''
      }else{
        return ''
      }
    },
    // 人员名称
    formatfpduser2: function(row, column) {
      if(this.atsUsers){
        for (let i = 0; i < this.atsUsers.length; i++) {
          if (row.userid.toString() == this.atsUsers[i].fuserno) {
            return this.atsUsers[i].username
          }
        }
        return ''
      }else{
        return ''
      }
    },
    // 搜索地点弹出框
    searchDialoglab(value) {
      this.labFlag = value
      if(value === 'add'){
        this.labDialogFsiteNo = this.dataForm.fsiteNo
      }else{
        this.labDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisiblelab = true
    },

    // 地点弹出框返回值
    labDialogValue(value){
      if(this.labFlag==='add'){
        this.dataForm.fpointNo = value.lab.fpointNo
        this.listqualityEqt(value.lab.fpointNo)
      }else{
        this.listQuery.fpointNo = value.lab.fpointNo
      }
    },

    fsiteNoChange(data){
      this.dataForm.fpointNo=''
      this.searchListlab=[]
      this.getListfpointNo = []
      if(data){
        this.getFsiteNo=data
        this.getSearchListlab(data)
        getPointOptions(data)
          .then(response => {
            this.getListfpointNo = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNo = []
          })
      }
    },
    fsiteNoQueChange(data){
      this.listQuery.fpointNo=''
      this.searchListlab=[]
      this.getListfpointNoQuery = []

      if(data){
        this.getFsiteNo=data
        getPointOptions(data)
          .then(response => {
            this.getListfpointNoQuery = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNoQuery = []
          })
      }

    },
    // 地点获取搜索列表
    getSearchListlab(data) {
      this.listLoadingSearchlab = true
      if(data){
        const temp = {
          fsiteNo:data,
          fpointFlag:4,
          page:this.listQuerySearchlab.page,
          limit:this.listQuerySearchlab.limit,
          sort: 'create_time',
          order: 'desc',
          fpointName:this.listQuerySearchlab.fpointName,
        }
        listRoom(temp)
          .then(response => {
            this.searchListlab = response.data.data.list
            this.totalSearchlab = response.data.data.total
            this.listLoadingSearchlab = false
          })
          .catch(() => {
            this.searchListlab = []
            this.totalSearchlab = 0
            this.listLoadingSearchlab = false
          })
      }else{
        this.searchListlab = []
      }
    },
    // 物料弹出框
    searchDialogMaterial() {
      this.materialIdVisible = true;
      this.materialFsiteNo = this.dataForm.fsiteNo;
    },

    // 物料弹出框
    materialDialogValue(value) {
      this.dataForm1.fgoodno = value.data.fgoodno;
      this.dataForm1.fgoodname = value.data.fgoodname;
      // this.dataForm1.fbuyprice = value.data.fbuyprice;
      // if (value.data.fbalqty) {
      //   this.dataForm1.fbalqty = value.data.fbalqty;
      // } else {
      //   this.dataForm1.fbalqty = 0;
      // }
      // this.dataForm1.frfidbarcode = 0;
      // this.dataForm1.fuseqty = 0;
      // this.dataForm1.fboxbarcode = 0;
      // this.dataForm1.fcabbarcode = 0;
    },
    // 物料
    formatfgoodno: function (row, column) {
      if (this.goodAllList.length > 0) {
        for (let i = 0; i < this.goodAllList.length; i++) {
          if (row.fgoodno === this.goodAllList[i].value) {
            return this.goodAllList[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    // 地点获取搜索列表
    getSearchListlabPage(data) {
      this.listLoadingSearchlab = true
      let queryFsiteNo
      if(data){
        if(this.searchFlaglab==='add'){
          // queryFsiteNo = this.dataForm.fsiteNo
          queryFsiteNo = this.$store.state.user.fsiteNo
        }else{
          // queryFsiteNo = this.listQuery.fsiteNo
          queryFsiteNo = this.$store.state.user.fsiteNo
        }
        const temp = {
          fsiteNo:queryFsiteNo,
          fpointFlag:4,
          page:this.listQuerySearchlab.page,
          limit:this.listQuerySearchlab.limit,
          sort: 'create_time',
          order: 'desc',
          fpointName:this.listQuerySearchlab.fpointName,
        }
        listRoom(temp)
          .then(response => {
            this.searchListlab = response.data.data.list
            this.totalSearchlab = response.data.data.total
            this.listLoadingSearchlab = false
          })
          .catch(() => {
            this.searchListlab = []
            this.totalSearchlab = 0
            this.listLoadingSearchlab = false
          })
      }else{
        this.searchListlab = []
      }
    },


    // 地点获取搜索列表
    searchFilterlab() {
      this.listQuerySearchlab.page = 1
      this.getSearchListlab(this.getFsiteNo)
    },
    // 地点获取搜索列表
    searchDatalab(row) {
      this.searchVisiblelab = false
      this.listQuerySearchlab.page = 1
      if(this.searchFlaglab==='add'){
        this.dataForm.fpointNo = row.fpointNo
      }else{
        this.listQuery.fpointNo = row.fpointNo
      }
    },

    // 搜索地点获取焦点
    fpointNoFocus(){
      if(!this.listQuery.fsiteNo){
        this.getListfpointNoQuery = []
      }
    },

    getList() {
      this.listLoading = true
      this.listQuery.fbillstate = 1
      this.listQuery.fseeuser = this.$store.state.user.id
      listplanHead(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fworkmemo: undefined,
        fplandate: undefined,
        fplanuser: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fexplanation: undefined,
        foperator: undefined,
        fupddate: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        id: undefined,
        details: [],
        details2: []
      }
    },
    parserDate(date) {
      var t = Date.parse(date)
      if (!isNaN(t)) {
        return new Date(Date.parse(date.replace(/-/g, '/')))
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
      const day3 = new Date();
      day3.setTime(day3.getTime()+24*60*60*1000);
      const tem = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
      this.dataForm.fplandate=this.parserDate(tem)
      this.dataForm1.funit = 0
      this.dataForm1.fspeed = 1
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getFbillno({ flag: 2027 })
        .then((response) => {
          if (response) {
            this.dataForm.fbillno = response.data.data
            this.dialogFormVisible = true
          }
        })
        .catch(() => {
          this.dataForm.fbillno = ''
        })
      listUserByFsiteNo(this.dataForm.fsiteNo)
        .then(response => {
          this.atsUsers = response.data.data.list
        })
        .catch(() => {
          this.atsUsers = []
        })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.fbillstate=1
          if(this.dataForm.details.length > 0){
             createplanHead(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加智量仪工作计划主表成功'
              })
              this.selectList = [];
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
          }else{
              this.$notify.error({
                title: '失败',
                message: "物料列表不能为空"
              })
          }

        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.detailsTmp = []
      row.details.forEach(v => {
        this.detailsTmp.push(v)
        this.selectList.push(v.fequipmentbarcode)
      })
      this.detailsTmp2 = []
      row.details2.forEach(v => {
        this.detailsTmp2.push(v)
      })
      this.getSearchListlab(row.fsiteNo)
      getPointOptions(row.fsiteNo)
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
      this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
      listqualityEqtByFpointNo(row.fpointNo)
        .then(response => {
          this.details2 = response.data.data.list
          this.zlyList = response.data.data.list
        })
        .catch(() => {
          this.details2 = []
          this.zlyList = []
        })
      listUserByFsiteNo(this.dataForm.fsiteNo)
        .then(response => {
          this.atsUsers = response.data.data.list
        })
        .catch(() => {
          this.atsUsers = []
        })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    listqualityEqt (fpointNo) {
      this.dataForm1.fequipmentbarcode = ''
      this.zlyList = []
      if(fpointNo){
           listqualityEqtByFpointNo(fpointNo)
        .then(response => {
          this.details2 = response.data.data.list
          this.zlyList = response.data.data.list
        })
        .catch(() => {
          this.details2 = []
          this.zlyList = []
        })
      }
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if(this.dataForm.details.length > 0){
             updateplanHead(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新智量仪工作计划主表成功'
              })
               this.selectList = [];
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
          }else{
              this.$notify.error({
                title: '失败',
                message: "物料列表不能为空"
              })
          }

        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除智量仪工作计划主表', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteplanHead(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除智量仪工作计划主表成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
  audit(row) {
    this.$confirm('是否确定', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.dataForm = Object.assign({}, row)
      checkBiz({ pBillNo: this.dataForm.fbillno ,pBillState:2 ,pBizType: 'i_tc_plan_head', pUser:this.$store.state.user.id })
        .then(() => {
          for (const v of this.list) {
            if (v.fbillno === this.dataForm.fbillno) {
              this.dataForm.fbillstate = 2
              const index = this.list.indexOf(v)
              this.list.splice(index, 1, this.dataForm)
              break
            }
          }
          this.$notify.success({
            title: '成功',
            message: '确认成功'
          })
          this.getList()
        })
        .catch((response) => {
          this.$notify.error({
            title: '失败',
            message: response.data.data
          })
        })
    })
  },
    resetForm1(){
      this.dataForm1 = {
        fequipmentbarcode: undefined,
        fgoodno: undefined,
        fgoodname: undefined,
        fspeed: undefined,
        funit: undefined,
        fsetweight: undefined,
      }
    },
    addData() {
      this.dialogFormVisible1 = false;
      // for (var i in this.dataForm.actions) {
      //   var eqt = this.dataForm.actions[i]
      //   if (eqt.faction === this.dataForm1.faction) { //重复不添加
      //     return
      //   }
      // }
       this.$refs['dataForm1'].validate(valid => {
        if (valid) {
             // 判断智量仪是否重复
            const tem = this.selectList.indexOf(this.dataForm1.fequipmentbarcode)
            if(tem<0){
                this.dataForm.details.push(this.dataForm1);
              this.selectList.push(this.dataForm1.fequipmentbarcode)
              this.resetForm1()
              // 清除验证
              this.$nextTick(() => {
                this.$refs["dataForm1"].clearValidate();
              });
              this.dataForm1.funit = 0
              this.dataForm1.fspeed = 1
            }else{
              this.$notify.error({
                    title: '失败',
                    message: '智量仪重复'
                  })
              return;
            }

        }
       })

      // this.dataForm.actions.push(
      //   { fairitem: this.dataForm1.fairitem.value, fairitemname: this.dataForm1.fairitem.label, fwhere: this.sTcSceneItem.fwhere, fwhere2: this.sTcSceneItem.fwhere2, fvalue: this.sTcSceneItem.fvalue, fvalue2: this.sTcSceneItem.fvalue2 })
    },
    cancle(){
        this.resetForm1()
      // 清除验证
      this.$nextTick(() => {
        this.$refs["dataForm1"].clearValidate();
      });
      this.dataForm1.funit = 0
      this.dataForm1.fspeed = 1
    },
    resetForm2(){
      this.dataForm2 = {
        fuserno: undefined,
        username: undefined,
      }
    },
    addData2() {
      this.dialogFormVisible2 = false
      for (var i in this.multipleSelection) {
        var atsUser = this.multipleSelection[i]
        var canAdd = true
        for (var j in this.dataForm.details2) {
          if (atsUser.fuserno + '' === this.dataForm.details2[j].userid.toString()) {
            canAdd = false
            break
          }
        }
        if (canAdd) {
          atsUser.fbillno = this.dataForm.fbillno
          atsUser.userid = atsUser.fuserno
          this.dataForm.details2.push(atsUser)
          // this.multiple.push(atsUser)
        }
      }
      this.$refs.multipleTable.clearSelection()
      this.resetForm2()
        // 清除验证
        this.$nextTick(() => {
          this.$refs["dataForm2"].clearValidate();
        });
    },
    cancle1(){
        this.resetForm2()
        // 清除验证
        this.$nextTick(() => {
          this.$refs["dataForm2"].clearValidate();
        });
        this.$refs.multipleTable.clearSelection()
    },
    cancel() {
      this.dialogFormVisible = false;
      this.selectList = [];
      this.zlyList = []
      this.list.forEach((v) => {
        if (v.fbillno === this.dataForm.fbillno) {
          // this.dataForm = this.listTmp;
          this.dataForm.details = this.detailsTmp;
          this.dataForm.details2 = this.detailsTmp2;
          const index = this.list.indexOf(v);
          this.list.splice(index, 1, this.dataForm);
          return;
        }
      });
      this.cancle()
    },
        // 弹窗点击关闭前的回调
    handleClose() {
      this.dialogFormVisible = false;
      this.selectList = [];
      this.zlyList = []
      this.list.forEach((v) => {
        if (v.fbillno === this.dataForm.fbillno) {
          // this.dataForm = this.listTmp;
          this.dataForm.details = this.detailsTmp;
          this.dataForm.details2 = this.detailsTmp2;
          const index = this.list.indexOf(v);
          this.list.splice(index, 1, this.dataForm);
          return;
        }
      });
       this.cancle()
    },
    handleUpdate1() {
      this.dialogStatus1 = "update";
      this.dialogFormVisible1 = true;
      // this.multipleSelection = []
      this.dataForm1 = {};
      this.$nextTick(() => {
        this.$refs["dataForm1"].clearValidate();
      });
    },
    handleUpdate2(fpointNo) { //todo
      if (fpointNo == null) {
        this.$notify.info({
          title: '',
          message: '请先选择实验室'
        })
        return
      }
      listqualityEqtByFpointNo(fpointNo)
        .then(response => {
          this.details2 = response.data.data.list
          this.zlyList = response.data.data.list
        })
        .catch(() => {
          this.details2 = []
          this.zlyList = []
        })
      this.dialogStatus2 = 'update'
      this.dialogFormVisible2 = true
      this.multipleSelection = []
      this.$nextTick(() => {
        this.$refs['dataForm2'].clearValidate()
      })
    },
    handleDelete1(row) {
      const index = this.dataForm.details.indexOf(row);
      this.dataForm.details.splice(index, 1);
      this.selectList.splice(index, 1);
      // this.dataForm.actions.pop(row)
    },
    handleDelete2(row) {
      const index = this.dataForm.details2.indexOf(row);
      this.dataForm.details2.splice(index, 1);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
